<?php
class TaxonomyController extends Controller{
	function __construct(){
		parent::__construct();
		$this->taxonomyAction = new TaxonomyAction();
	}
	
	function index(){
		$this->smarty->display('admin/taxonomy/index.tpl');
	}
	
	function vocabulary_add(){
		$this->smarty->display('admin/taxonomy/vocabulary_add.tpl');
	}
	
	function vocabulary_submit(){
		$fields = array(
			'name' => '名称不能为空',
			'machine_name' => '机读名称不能为空',
		);
		$cp = check_post(array_keys($fields));
		if(!($cp === '1')){
			exit($fields[$cp]);
		}
		
		$result = $this->taxonomyAction->vocabularyAdd($_POST);
		switch ($result){
			case 1:
				$msg = 'success';
				break;
			case -1:
				$msg = '添加失败';
				break;
			case -2:
				$msg = '机读名称重复';
				break;
			default:
				$msg = '未知的错误';
		}
		exit($msg);
	}
	function vocabulary_update(){
		$fields = array(
				'name' => '名称不能为空',
				'machine_name' => '机读名称不能为空',
		);
		$cp = check_post(array_keys($fields));
		if(!($cp === '1')){
			exit($fields[$cp]);
		}
		
		$result = $this->taxonomyAction->vocabularyUpdate($_POST);
		switch ($result){
			case 1:
				$msg = 'success';
				break;
			case -1:
				$msg = '修改失败';
				break;
			case -2:
				$msg = '机读名称重复';
				break;
			default:
				$msg = '未知的错误';
		}
		exit($msg);
	}
	
	function vocabulary_list(){
		$this->smarty->display('admin/taxonomy/vocabulary_list.tpl');
	}
	
	function vocabulary_list_data(){
		$result = $this->taxonomyAction->vocabularyListJson();
		exit($result);
	}
	
	function vocabulary_delete(){
		$vid = intval($_POST['vid']);
		if($vid){
			$result = $this->taxonomyAction->vocabularyDel($vid);
			exit("$result");
		}else{
			exit('-3');
		}
	}
	
	function vocabulary_get(){
		$vid = intval($_POST['vid']);
		if($vid){
			$result = $this->taxonomyAction->vocabularyGetOne($vid);
			exit(json_encode($result));
		}else{
			exit('-1');
		}
	}
	
	function term_add(){
		$vid = intval($_GET['vid']);
		$tid = intval($_GET['tid']);
		if($vid){
		    $vac = $vid? $this->taxonomyAction->vocabularyGetOne($vid): null;
    		$terms = $vac? $this->taxonomyAction->termList($vac['vid']): null;
		}else{
		    $vocabularies = $this->taxonomyAction->vocabularyList();
		}
		if($tid){
		    $term = $this->taxonomyAction->getTerm($tid);
		}
		
		
		
		
		$this->smarty->assign('vac', $vac);
		$this->smarty->assign('terms', $terms);
		$this->smarty->assign('term', $term);
		$this->smarty->assign('vocabularies', $vocabularies);
		$this->smarty->display('admin/taxonomy/term_add.tpl');
	}
	
	function term_submit(){
		$fields = array(
			'name' => '名称不能为空',
			'vid' => '请选择分类',
		);
		$cp = check_post(array_keys($fields));
		if(!($cp === '1')){
			exit($fields[$cp]);
		}
		
		$result = $this->taxonomyAction->termAdd($_POST);
		switch ($result){
			case 1:
				$msg = 'success';
				break;
			case -1:
				$msg = '添加失败';
				break;
			default:
				$msg = '未知的错误';
		}
		exit($msg);
		
	}
	
	function term_list(){
		$vid = intval($_GET['vid']);
		$parent = intval($_GET['parent'])? intval($_GET['parent']): null;
		
		$this->smarty->assign('vid', $vid);
		$this->smarty->assign('parent', $parent);
		$this->smarty->display('admin/taxonomy/term_list.tpl');
	}
	
	function term_list_data(){
		$vid = intval($_GET['vid']);
		$parent = $_GET['parent'] === ''?  null: intval($_GET['parent']);
		if($vid)
			$result = $this->taxonomyAction->termListJson($vid, $parent);
		exit($result);
	}
	
	function term_delete(){
		$tid = intval($_POST['tid']);
		if($tid){
			$result = $this->taxonomyAction->termDel($tid);
			exit("$result");
		}else{
			exit('-3');
		}
	}
	
	function get_child_terms_options_html(){
		$parent = intval($_POST['parent']);
		$term = $this->taxonomyAction->getTerm($parent);
		$terms = $this->taxonomyAction->termList($term['vid'], $term['tid']);
		$html = '';
		foreach($terms as $t){
			$html .= "<option value=\"{$t['tid']}\">{$t['name']}</option>";
		}
		exit($html);
	}
}